package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.facade.AccLoanStatisticService;
import com.fdb.efp.report.service.facade.AccLoanStatisticTempService;
import com.fdb.efp.report.service.facade.RptAccLoanStatisticService;
import com.fdb.efp.report.service.vo.AccLoanStatisticTempVo;
import com.fdb.efp.report.service.vo.RptAccLoanStatisticTempVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 需求编号:2020D0554
 * 问题编号:
 * 开发人员: wengqw
 * 创建日期:2020/6/16
 * 功能描述:
 * 修改日期:2020/6/16
 * 修改描述:
 */
@Service("accLoanStatisticService")
public class AccLoanStatisticServiceImpl implements AccLoanStatisticService, FrameworkService {

    private Logger logger = LoggerFactory.getLogger(AccLoanStatisticServiceImpl.class);
    private final static String channelCodeMs = "04";
    private final static String channelCodeBd = "05";
    private final static String channelMameMs = "马上";
    private final static String channelMameBd = "度小满";
    private final int startTotalBalance = 0;
    private final int fiveTHSTotalBalance = 5000;
    private final int tenTHSTotalBalance = 10000;
    private final int fiftyTHSTotalBalance = 50000;
    private final int oneHundredTHSTotalBalance = 100000;
    private final int oneHundredAndFiftyTHSTotalBalance = 150000;
    private final int twoHundredTHSTotalBalance = 200000;
    private final int twoHundredAndFiftyTHSTotalBalance = 250000;
    private final int threeHundredTHSTotalBalance = 300000;

    @Autowired
    @Qualifier("accLoanStatisticTempService")
    private AccLoanStatisticTempService accLoanStatisticTempService;

    @Autowired
    @Qualifier("rptAccLoanStatisticService")
    private RptAccLoanStatisticService rptAccLoanStatisticService;

    @Override
    public boolean accLoanStatistics() {
        boolean result = true;
        logger.info("统计未结清借据贷款余额开始");
        try {
            /**
             * 先从acc_loan表中查询回加工好的数据
             */
            logger.info("======>统计未结清借据贷款余额(马上金融)开始");
            //1.1账户为非结清且贷款余额不大于5000的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs1 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, startTotalBalance, fiveTHSTotalBalance);
            //2.1账户为非结清且贷款余额大于5000小于1万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs2 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, fiveTHSTotalBalance, tenTHSTotalBalance);
            //3.1账户为非结清且贷款余额大于1万小于5万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs3 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, tenTHSTotalBalance, fiftyTHSTotalBalance);
            //4.1账户为非结清且贷款余额大于5万小于10万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs4 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, fiftyTHSTotalBalance, oneHundredTHSTotalBalance);
            //5.1账户为非结清且贷款余额大于10万小于15万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs5 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, oneHundredTHSTotalBalance, oneHundredAndFiftyTHSTotalBalance);
            //6.1账户为非结清且贷款余额大于15万小于20万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs6 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, oneHundredAndFiftyTHSTotalBalance, twoHundredTHSTotalBalance);
            //7.1账户为非结清且贷款余额大于20万小于25万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs7 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, twoHundredTHSTotalBalance, twoHundredAndFiftyTHSTotalBalance);
            //8.1账户为非结清且贷款余额大于25万小于30万的用户账户贷款余额之和(马上)
            AccLoanStatisticTempVo accLoanStatisticMs8 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeMs, twoHundredAndFiftyTHSTotalBalance, threeHundredTHSTotalBalance);
            logger.info("======>统计未结清借据贷款余额(马上金融)结束");
            RptAccLoanStatisticTempVO rptAccLoanStatisticTempVO = new RptAccLoanStatisticTempVO();
            rptAccLoanStatisticTempVO.setChannelCode(channelCodeMs);
            rptAccLoanStatisticTempVO.setChannelName(channelMameMs);
            rptAccLoanStatisticTempVO.setFiveTHSTotalBalance(accLoanStatisticMs1.getTotalBalance());
            rptAccLoanStatisticTempVO.setTenTHSTotalBalance(accLoanStatisticMs2.getTotalBalance());
            rptAccLoanStatisticTempVO.setFiftyTHSTotalBalance(accLoanStatisticMs3.getTotalBalance());
            rptAccLoanStatisticTempVO.setOneHundredTHSTotalBalance(accLoanStatisticMs4.getTotalBalance());
            rptAccLoanStatisticTempVO.setOneHundredAndFiftyTHSTotalBalance(accLoanStatisticMs5.getTotalBalance());
            rptAccLoanStatisticTempVO.setTwoHundredTHSTotalBalance(accLoanStatisticMs6.getTotalBalance());
            rptAccLoanStatisticTempVO.setTwoHundredAndFiftyTHSTotalBalance(accLoanStatisticMs7.getTotalBalance());
            rptAccLoanStatisticTempVO.setThreeHundredTHSTotalBalance(accLoanStatisticMs8.getTotalBalance());
            rptAccLoanStatisticTempVO.setFiveTHSCountBalance(accLoanStatisticMs1.getCountBalance());
            rptAccLoanStatisticTempVO.setTenTHSCountBalance(accLoanStatisticMs2.getCountBalance());
            rptAccLoanStatisticTempVO.setFiftyTHSCountBalance(accLoanStatisticMs3.getCountBalance());
            rptAccLoanStatisticTempVO.setOneHundredTHSCountBalance(accLoanStatisticMs4.getCountBalance());
            rptAccLoanStatisticTempVO.setOneHundredAndFiftyTHSCountBalance(accLoanStatisticMs5.getCountBalance());
            rptAccLoanStatisticTempVO.setTwoHundredTHSCountBalance(accLoanStatisticMs6.getCountBalance());
            rptAccLoanStatisticTempVO.setTwoHundredAndFiftyTHSCountBalance(accLoanStatisticMs7.getCountBalance());
            rptAccLoanStatisticTempVO.setThreeHundredTHSCountBalance(accLoanStatisticMs8.getCountBalance());
            rptAccLoanStatisticTempVO.setFiveTHSCountCus(accLoanStatisticMs1.getCountCus());
            rptAccLoanStatisticTempVO.setTenTHSCountCus(accLoanStatisticMs2.getCountCus());
            rptAccLoanStatisticTempVO.setFiftyTHSCountCus(accLoanStatisticMs3.getCountCus());
            rptAccLoanStatisticTempVO.setOneHundredTHSCountCus(accLoanStatisticMs4.getCountCus());
            rptAccLoanStatisticTempVO.setOneHundredAndFiftyTHSCountCus(accLoanStatisticMs5.getCountCus());
            rptAccLoanStatisticTempVO.setTwoHundredTHSCountCus(accLoanStatisticMs6.getCountCus());
            rptAccLoanStatisticTempVO.setTwoHundredAndFiftyTHSCountCus(accLoanStatisticMs7.getCountCus());
            rptAccLoanStatisticTempVO.setThreeHundredTHSCountCus(accLoanStatisticMs8.getCountCus());
            //删除旧数据
            int num2 = rptAccLoanStatisticService.deletByDateAndChannelCode(channelCodeMs);
            //将加工完成的数据入库到 rpt_acc_loan_statistic_temp 表中
            int num = rptAccLoanStatisticService.insert(rptAccLoanStatisticTempVO);

            logger.info("======>统计未结清借据贷款余额(度小满)开始");
            //1.2账户为非结清且贷款余额不大于5000的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd1 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, startTotalBalance, fiveTHSTotalBalance);
            //2.2账户为非结清且贷款余额大于5000小于1万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd2 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, fiveTHSTotalBalance, tenTHSTotalBalance);
            //3.2账户为非结清且贷款余额大于1万小于5万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd3 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, tenTHSTotalBalance, fiftyTHSTotalBalance);
            //4.2账户为非结清且贷款余额大于5万小于10万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd4 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, fiftyTHSTotalBalance, oneHundredTHSTotalBalance);
            //5.2账户为非结清且贷款余额大于10万小于15万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd5 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, oneHundredTHSTotalBalance, oneHundredAndFiftyTHSTotalBalance);
            //6.2账户为非结清且贷款余额大于15万小于20万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd6 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, oneHundredAndFiftyTHSTotalBalance, twoHundredTHSTotalBalance);
            //7.2账户为非结清且贷款余额大于20万小于25万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd7 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, twoHundredTHSTotalBalance, twoHundredAndFiftyTHSTotalBalance);
            //8.2账户为非结清且贷款余额大于25万小于30万的用户账户贷款余额之和(度小满)
            AccLoanStatisticTempVo accLoanStatisticBd8 = accLoanStatisticTempService.getAccLoanStatistic(channelCodeBd, twoHundredAndFiftyTHSTotalBalance, threeHundredTHSTotalBalance);
            logger.info("======>统计未结清借据贷款余额(度小满)结束");
            RptAccLoanStatisticTempVO rptAccLoanStatisticTempVO1 = new RptAccLoanStatisticTempVO();
            rptAccLoanStatisticTempVO1.setChannelCode(channelCodeBd);
            rptAccLoanStatisticTempVO1.setChannelName(channelMameBd);
            rptAccLoanStatisticTempVO1.setFiveTHSTotalBalance(accLoanStatisticBd1.getTotalBalance());
            rptAccLoanStatisticTempVO1.setTenTHSTotalBalance(accLoanStatisticBd2.getTotalBalance());
            rptAccLoanStatisticTempVO1.setFiftyTHSTotalBalance(accLoanStatisticBd3.getTotalBalance());
            rptAccLoanStatisticTempVO1.setOneHundredTHSTotalBalance(accLoanStatisticBd4.getTotalBalance());
            rptAccLoanStatisticTempVO1.setOneHundredAndFiftyTHSTotalBalance(accLoanStatisticBd5.getTotalBalance());
            rptAccLoanStatisticTempVO1.setTwoHundredTHSTotalBalance(accLoanStatisticBd6.getTotalBalance());
            rptAccLoanStatisticTempVO1.setTwoHundredAndFiftyTHSTotalBalance(accLoanStatisticBd7.getTotalBalance());
            rptAccLoanStatisticTempVO1.setThreeHundredTHSTotalBalance(accLoanStatisticBd8.getTotalBalance());
            rptAccLoanStatisticTempVO1.setFiveTHSCountBalance(accLoanStatisticBd1.getCountBalance());
            rptAccLoanStatisticTempVO1.setTenTHSCountBalance(accLoanStatisticBd2.getCountBalance());
            rptAccLoanStatisticTempVO1.setFiftyTHSCountBalance(accLoanStatisticBd3.getCountBalance());
            rptAccLoanStatisticTempVO1.setOneHundredTHSCountBalance(accLoanStatisticBd4.getCountBalance());
            rptAccLoanStatisticTempVO1.setOneHundredAndFiftyTHSCountBalance(accLoanStatisticBd5.getCountBalance());
            rptAccLoanStatisticTempVO1.setTwoHundredTHSCountBalance(accLoanStatisticBd6.getCountBalance());
            rptAccLoanStatisticTempVO1.setTwoHundredAndFiftyTHSCountBalance(accLoanStatisticBd7.getCountBalance());
            rptAccLoanStatisticTempVO1.setThreeHundredTHSCountBalance(accLoanStatisticBd8.getCountBalance());
            rptAccLoanStatisticTempVO1.setFiveTHSCountCus(accLoanStatisticBd1.getCountCus());
            rptAccLoanStatisticTempVO1.setTenTHSCountCus(accLoanStatisticBd2.getCountCus());
            rptAccLoanStatisticTempVO1.setFiftyTHSCountCus(accLoanStatisticBd3.getCountCus());
            rptAccLoanStatisticTempVO1.setOneHundredTHSCountCus(accLoanStatisticBd4.getCountCus());
            rptAccLoanStatisticTempVO1.setOneHundredAndFiftyTHSCountCus(accLoanStatisticBd5.getCountCus());
            rptAccLoanStatisticTempVO1.setTwoHundredTHSCountCus(accLoanStatisticBd6.getCountCus());
            rptAccLoanStatisticTempVO1.setTwoHundredAndFiftyTHSCountCus(accLoanStatisticBd7.getCountCus());
            rptAccLoanStatisticTempVO1.setThreeHundredTHSCountCus(accLoanStatisticBd8.getCountCus());
            //删除旧数据
            int num3 = rptAccLoanStatisticService.deletByDateAndChannelCode(channelCodeBd);
            //将加工完成的数据入库到 rpt_acc_loan_statistic_temp 表中
            int num1 = rptAccLoanStatisticService.insert(rptAccLoanStatisticTempVO1);
            if (num == -1 || num1 == -1 || num2 == -1 || num3 == -1) {
                result = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            String remark = "加工数据入库- rpt_acc_loan_statistic_temp" + e.getMessage();
            logger.error(remark);
            result = false;
        }
        return result;
    }

}
